﻿using FluentMigrator;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseMigration
{
    public class BaseCreater : Migration
    {
        public override void Up()
        {

        }


        public override void Down()
        {
        }
        /// <summary>
        /// 创建数据表 
        /// </summary>
        /// <param name="ModelType"></param>
        protected void CreateTable(Type ModelType)
        {
            var Table = Create.Table(ModelType.Name);

            PropertyInfo[] propertyInfos = ModelType.GetProperties();

            foreach (var item in propertyInfos)
            {
                DescriptionAttribute dsa = item.GetCustomAttribute<DescriptionAttribute>();
                string descript = "";
                if (dsa != null) descript = dsa.Description;
                bool isKey = descript.ToLower().Contains("key");

                var Column = Table.WithColumn(item.Name);

                if (item.PropertyType.Name.ToLower() == "string")
                {
                    if (isKey) Column.AsString().PrimaryKey().NotNullable().WithColumnDescription(descript);
                    else Column.AsString().Nullable().WithColumnDescription(descript);
                }
                string boolString = "boolean;bool;";
                if (boolString.Contains(item.PropertyType.Name.ToLower() + ";")) Column.AsBoolean().WithColumnDescription(descript);

                if (item.PropertyType.IsEnum) { Column.AsInt16().WithColumnDescription(descript).WithDefaultValue(0); }//枚举变量
                else
                {
                    if (item.PropertyType.Name.ToLower() == "int" || item.PropertyType.Name.ToLower() == "int16" || item.PropertyType.Name.ToLower() == "uint16")
                    {
                        if (isKey) Column.AsInt16().Identity().PrimaryKey().NotNullable().WithColumnDescription(descript);
                        else Column.AsInt16().WithColumnDescription(descript).WithDefaultValue(0);
                    }
                    if (item.PropertyType.Name.ToLower() == "int32" || item.PropertyType.Name.ToLower() == "uint32")
                    {
                        if (isKey) Column.AsInt32().Identity().PrimaryKey().NotNullable().WithColumnDescription(descript);
                        else Column.AsInt32().WithColumnDescription(descript).WithDefaultValue(0);
                    }
                    if (item.PropertyType.Name.ToLower() == "int64" || item.PropertyType.Name.ToLower() == "uint64")
                    {
                        if (isKey) Column.AsInt64().Identity().PrimaryKey().NotNullable().WithColumnDescription(descript);
                        else Column.AsInt64().WithColumnDescription(descript).WithDefaultValue(0);
                    }
                }
                if (item.PropertyType.Name.ToLower() == "double" || item.PropertyType.Name.ToLower() == "single")
                {
                    Column.AsFloat().WithColumnDescription(descript).WithDefaultValue(0);
                }
                string numString = "datetime;date;";
                if (numString.Contains(item.PropertyType.Name.ToLower() + ";"))
                {
                    Column.AsDateTime().Nullable().WithColumnDescription(descript);
                }

            }
        }
       

    }
}
